/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sitech.portal.server.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.server.service.ICalendarService;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.util.CommUtil;

/**
 *
 * @author Crixus
 */
public class CalendarAction extends JGridBaseAction {

	private ICalendarService calendarService;
	private String flag ;
	
    public ICalendarService getCalendarService() {
		return calendarService;
	}

	public void setCalendarService(ICalendarService calendarService) {
		this.calendarService = calendarService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	/**
     * 我的日历（任务计划）
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public String calendar() throws Exception{
        // 取得用户LOGIN_ID
        TblSYS_LOGINMSG user = this.getCurrentUser();
        List<Map> events = calendarService.findCalendarEvents(user.getLOGIN_ID());
        StringBuilder json = new StringBuilder();
        for (int i = 0; i < events.size(); i++) {
            Map event = events.get(i);
            String start = null, end = null;
            String start_time = (String) event.get("START_TIME");
            String end_time = (String) event.get("END_TIME");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (start_time != null) {
                start = "new Date(" + dateFormat.parse(start_time).getTime() + ")";
            }
            if (end_time != null) {
                end = "new Date(" + dateFormat.parse(end_time).getTime() + ")";
            }
            json.append(",{id:'").append(event.get("ID")).append("',title:'").append(event.get("TITLE")).append("', allDay:").append("1".equals(event.get("ALLDAY"))).append(",description:'").append(event.get("DESCRIPTION")).append("', notice:'").append(event.get("NOTICE")).append("',start:").append(start).append(", end:").append(end).append("}");
        }
        if(json.length()>0){
            json.deleteCharAt(0);
        }
        flag = "[" + json + "]";
        return SUCCESS;
    }

    public String addCalendarEvent() {
        HashMap event = new HashMap();
        for (int i = 0; i < CommUtil.EVENT_PARAMS.size(); i++) {
            String key = CommUtil.EVENT_PARAMS.get(i);
            event.put(key, this.getRequest().getParameter(key));
        }
        // 取得用户LOGIN_ID
        TblSYS_LOGINMSG user = this.getCurrentUser();
        String id = calendarService.insertCalendarEvent(event, user.getLOGIN_ID());
        flag = "{id:'" + id + "'}";
        return SUCCESS;
    }

    public String editCalendarEvent() {
    	flag = "true";
    	try{
	        HashMap event = new HashMap();
	        for (int i = 0; i < CommUtil.EVENT_PARAMS.size(); i++) {
	            String key = CommUtil.EVENT_PARAMS.get(i);
	            event.put(key, this.getRequest().getParameter(key));
	        }
	        // 取得用户LOGIN_ID
	        TblSYS_LOGINMSG user = this.getCurrentUser();
	        calendarService.updateCalendarEvent(event, user.getLOGIN_ID());
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
        return SUCCESS;
    }

    public String removeCalendarEvent() {
    	flag = "true";
    	try{
	        // 取得用户LOGIN_ID
	        TblSYS_LOGINMSG user = this.getCurrentUser();
	        calendarService.deleteCalendarEvent(this.getRequest().getParameter("id"), user.getLOGIN_ID());
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
        return SUCCESS;
    }
}
